//
//  MusicListTableViewCell.m
//  MountEverestFM
//
//  Created by ChenNing on 15/10/30.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "BestMusicListTableViewCell.h"


@interface BestMusicListTableViewCell()
// 曲目图
@property(nonatomic,strong)UIImageView *fmImageView;
// 曲目图上的播放图
@property(nonatomic,strong)UIImageView *fmPlayImageView;
// 曲目图上的暂停图
@property(nonatomic,strong)UIImageView *fmPauseImageView;
// 标题
@property(nonatomic,strong)UILabel *fmTitleLabel;

// 播放数量的图标
@property(nonatomic,strong)UIImageView *fmPlayCountImageView;
// 播放数量
@property(nonatomic,strong)UILabel *fmPlayCountLabel;

// 播放时长的图标
@property(nonatomic,strong)UIImageView *fmTimerImageView;
// 播放时长
@property(nonatomic,strong)UILabel *fmTimerLabel;

// 集数的图标
@property(nonatomic,strong)UIImageView *fmCountImageView;
// 集数
@property(nonatomic,strong)UILabel *fmCountLabel;

// 分割线
@property(nonatomic,strong)UIImageView *fmLineImageView;
@end

@implementation BestMusicListTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.fmImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_fmImageView];
        
        self.fmPlayImageView = [[UIImageView alloc] init];
        [self.fmImageView addSubview:_fmPlayImageView];
        
        self.fmPauseImageView = [[UIImageView alloc] init];
        [self.fmImageView addSubview:_fmPauseImageView];
        
        self.fmTitleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_fmTitleLabel];
        
        self.fmPlayCountImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_fmPlayCountImageView];
        
        self.fmPlayCountLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_fmPlayCountLabel];
        
        self.fmTimerImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_fmTimerImageView];
        
        self.fmTimerLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_fmTimerLabel];
        
        self.fmCountImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_fmCountImageView];
        
        self.fmCountLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_fmCountLabel];
        
        self.fmLineImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_fmLineImageView];
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    // 曲目图
    self.fmImageView.frame = CGRectMake(FM_Screen_Pts_Width*10, FM_Screen_Pts_Height*10, FM_Screen_Pts_Width*70, FM_Screen_Pts_Width*70);
    self.fmImageView.layer.cornerRadius = FM_Screen_Pts_Width*35;
    self.fmImageView.layer.masksToBounds = YES;
    
    // 曲目图上的播放图
    self.fmPlayImageView.frame = CGRectMake(FM_Screen_Pts_Width*25, FM_Screen_Pts_Width*25, FM_Screen_Pts_Width*20, FM_Screen_Pts_Width*20);
    // 曲目图上的暂停图
    self.fmPauseImageView.frame = CGRectMake(FM_Screen_Pts_Width*25, FM_Screen_Pts_Width*25, FM_Screen_Pts_Width*20, FM_Screen_Pts_Width*20);
    
    // 标题
    self.fmTitleLabel.frame = CGRectMake(FM_Screen_Pts_Width*90, FM_Screen_Pts_Height*15, FM_Screen_Width-FM_Screen_Pts_Width*100, FM_Screen_Pts_Height*30);
    self.fmTitleLabel.font = [UIFont systemFontOfSize:15];
    self.fmTitleLabel.numberOfLines = 2;
    [self.fmTitleLabel sizeToFit];
    
    // 播放次数图标
    self.fmPlayCountImageView.frame = CGRectMake(FM_Screen_Pts_Width*90, self.fmImageView.frame.origin.y+self.fmImageView.frame.size.height-FM_Screen_Pts_Width*20, FM_Screen_Pts_Width*15, FM_Screen_Pts_Width*15);
    // 播放次数
    self.fmPlayCountLabel.frame = CGRectMake(FM_Screen_Pts_Width*108, self.fmImageView.frame.origin.y+self.fmImageView.frame.size.height-FM_Screen_Pts_Width*20-FM_Screen_Pts_Height*6, FM_Screen_Pts_Width*100, FM_Screen_Pts_Height*30);
    self.fmPlayCountLabel.font = [UIFont systemFontOfSize:13];
    self.fmPlayCountLabel.textColor = [UIColor grayColor];
    
    // 播放时长图标
    self.fmTimerImageView.frame = CGRectMake(FM_Screen_Pts_Width*200,  self.fmImageView.frame.origin.y+self.fmImageView.frame.size.height-FM_Screen_Pts_Width*20, FM_Screen_Pts_Width*15, FM_Screen_Pts_Width*15);
    // 播放时长
    self.fmTimerLabel.frame = CGRectMake(FM_Screen_Pts_Width*218,  self.fmImageView.frame.origin.y+self.fmImageView.frame.size.height-FM_Screen_Pts_Width*20-FM_Screen_Pts_Height*6, FM_Screen_Pts_Width*100, FM_Screen_Pts_Height*30);
    self.fmTimerLabel.font = [UIFont systemFontOfSize:13];
    self.fmTimerLabel.textColor = [UIColor grayColor];
    
    // 集数图标
    self.fmCountImageView.frame = CGRectMake(FM_Screen_Pts_Width*200,  self.fmImageView.frame.origin.y+self.fmImageView.frame.size.height-FM_Screen_Pts_Width*20, FM_Screen_Pts_Width*15, FM_Screen_Pts_Width*15);
    // 集数
    self.fmCountLabel.frame = CGRectMake(FM_Screen_Pts_Width*218,  self.fmImageView.frame.origin.y+self.fmImageView.frame.size.height-FM_Screen_Pts_Width*20-FM_Screen_Pts_Height*6, FM_Screen_Pts_Width*100, FM_Screen_Pts_Height*30);
    self.fmCountLabel.font = [UIFont systemFontOfSize:13];
    self.fmCountLabel.textColor = [UIColor grayColor];
    
    // 分割线
    self.fmLineImageView.frame =CGRectMake(0, 0, FM_Screen_Width, 1);
    
}


// 重写set方法
- (void)setBodyDictionary:(NSDictionary *)bodyDictionary
{
    if (_bodyDictionary != bodyDictionary) {
        _bodyDictionary = bodyDictionary;
        [self setBodyInfo:bodyDictionary];
    }
}

// 设置曲目列表数据
- (void)setBodyInfo:(NSDictionary *)bodyDictionary
{
    NSURL *imageViewUrl = [NSURL URLWithString:[bodyDictionary objectForKey:@"albumCoverUrl290"]];
    if(imageViewUrl == nil){
        imageViewUrl = [NSURL URLWithString:[bodyDictionary objectForKey:@"coverSmall"]];
        [self.fmImageView sd_setImageWithURL:imageViewUrl placeholderImage:[UIImage imageNamed:@"find_kind_btn_default@3x.png"]];
        
        NSLog(@"########%@",bodyDictionary);
        NSLog(@"^^^^^^^^%@",[NSURL URLWithString:[bodyDictionary objectForKey:@"coverPathBig"]]);

    }else{
        
   
    [self.fmImageView sd_setImageWithURL:imageViewUrl placeholderImage:[UIImage imageNamed:@"find_kind_btn_default@3x.png"]];
    }
    self.fmPlayImageView.image = [UIImage imageNamed:@"cell_sound_play_n@2x.png"];
    self.fmPauseImageView.image = [UIImage imageNamed:@"cell_sound_pause_n@2x.png"];
    
    self.fmTitleLabel.text = [bodyDictionary objectForKey:@"title"];
    
    CGFloat playCount = [[bodyDictionary objectForKey:@"playsCounts"] floatValue];
    if (playCount > 1) {
        self.fmPlayCountImageView.image = [UIImage imageNamed:@"sound_playtimes@3x.png"];
        if ((playCount / 10000) > 1) {
            self.fmPlayCountLabel.text = [NSString stringWithFormat:@"%.2f 万",(playCount / 10000)];
        }else{
            self.fmPlayCountLabel.text = [NSString stringWithFormat:@"%.0f", playCount];
        }
    }
    
    NSString *fmCount = [[bodyDictionary objectForKey:@"tracksCounts"] stringValue];
    if(fmCount == nil){
        fmCount =  [[bodyDictionary objectForKey:@"playCounts"] stringValue];
    }
    
    if(fmCount!=nil){
        // 集数
        self.fmCountImageView.image = [UIImage imageNamed:@"album_tracks@3x.png"];
        self.fmCountLabel.text = [NSString stringWithFormat:@"%@ 集",fmCount];
    }
    else
    {
        // 时长
        self.fmTimerImageView.image = [UIImage imageNamed:@"search_hist_tag@2x.png"];
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        formatter.dateFormat = @"mm:ss";
        NSTimeInterval longTimer = [[bodyDictionary objectForKey:@"duration"] doubleValue];
        NSDate *totelDate = [NSDate dateWithTimeIntervalSince1970:longTimer];
        self.fmTimerLabel.text = [formatter stringFromDate:totelDate];
    }
    
    

    
    self.fmLineImageView.image = [UIImage imageNamed:@"cell_contactlist_line@2x.png"];
}


@end
